package com.mogu_soup.util;

import com.mogu_soup.entity.Tank;

import java.util.Vector;

/**
 * Version: 2.1
 * Author: TangJun
 * Date: 2024-08-03
 * Note:坦克对象池
 */
public class TankPool {
    private static final int DEFAULT_SIZE = 20;//默认坦克池大小
    private static final int MAX_SIZE = 50;//
    private static Vector<Tank> pool;//坦克池
    //用静态代码块初始化，在类加载时就创建坦克对象
    static {
        pool = new Vector<Tank>(DEFAULT_SIZE);
        for (int i = 0; i < DEFAULT_SIZE; i++) {
            pool.add(new Tank(0,0,0));
        }
    }

    //获取坦克池中对象
    public static synchronized Tank removeTank() {
        return pool.isEmpty() ? new Tank(0,0,0) : pool.remove(0);
    }

    //归还坦克池中对象
    public static synchronized void addTank(Tank tank) {
        if (pool.size() < MAX_SIZE) {
            pool.add(tank);
        }
    }
}
